/*
 
Enterprise Social Network (ESN) is an open-source Java EE framework for
a social network application.

Copyright (C) 2006-07 (see CONTRIBUTORS file in project root)

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

*/

package esn.core.ejb.entity;

import java.io.*;

import javax.persistence.*;

/**
 * <p>Representation of a directed edge in a bi-directed relation
 * in the social network graph.</p>
 * 
 * @author Anirban Basu
 *
 */
@Entity
public class ESNEdge implements Serializable {
	private static final long serialVersionUID = 1L;
	private UserProfile source;
	private UserProfile destination;
	private double trust = 1.0D; //range = (0 1]
	//could attach more parameters, such as a "reason" behind the
	//relation - similar to Facebook
	
	/*
	 * TODO: Should ESNEdge have a long-based id? Or should it have a
	 * composite primary key formed from source and destination
	 * UserProfileWS ids?
	 * 
	 */
	
	private long id;
	
	/**
	 * Accessor for the destination vertex of this edge.
	 * 
	 * @return
	 */
	@ManyToOne
	@JoinColumn(name="destinationVertex")
	public UserProfile getDestination() {
		return destination;
	}
	/**
	 * Mutator for the destination vertex of this edge.
	 * 
	 * @param destination
	 */
	public void setDestination(UserProfile destination) {
		this.destination = destination;
	}
	
	/**
	 * Accessor for the source vertex of this edge.
	 * 
	 * @return
	 */
	@ManyToOne
	@JoinColumn(name="sourceVertex")
	public UserProfile getSource() {
		return source;
	}
	/**
	 * Mutator for the source vertex of this edge.
	 * 
	 * @param source
	 */
	public void setSource(UserProfile source) {
		this.source = source;
	}
	
	/**
	 * Accessor for the trust score on this edge.
	 * 
	 * @return
	 */
	@JoinColumn(nullable=false)
	public double getTrust() {
		return trust;
	}
	
	/**
	 * Mutator for the trust score on this edge.
	 * @param trust
	 */
	public void setTrust(double trust) {
		if(trust > 0 && trust <=1) {
			this.trust = trust;
		}
		else throw new NumberFormatException("Trust value has an acceptable range: (0.0D 1.0D]");
	}
	
	/**
	 * Accessor for the primary key.
	 * 
	 * @return
	 */
	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	public long getId() {
		return id;
	}
	
	/**
	 * Mutator for the primary key.
	 * 
	 * @param id
	 */
	public void setId(long id) {
		this.id = id;
	}
}
